﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace QAToolsAddin.Components {
    public partial class ExcelListWindows : UserControl {
        public event EventHandler SelectionChanged;

        private bool _disableEvents;

        public ExcelListWindows() {
            InitializeComponent();
        }

        private void ExcelListWindows_Load(object sender, EventArgs e) {
            if (ParentForm is ExcelForm == false)
                throw new ArgumentException("An ExcelForm parent type is required.");
            if (((ExcelForm)ParentForm).ExcelApp != null) {
                _disableEvents = true;
                comboBox1.SuspendLayout();
                foreach (Excel.Window win in ((ExcelForm)ParentForm).ExcelApp.Windows)
                    if (win.Visible)
                        comboBox1.Items.Add(win);
                comboBox1.ResumeLayout();
                _disableEvents = false;
            }
        }

        public void SetWindow(Excel.Workbook wb, Excel.Window wn) {
            SelectedWindow = wn;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Excel.Window SelectedWindow {
            get {
                return (Excel.Window)comboBox1.SelectedItem;
            }
            set {
                _disableEvents = true;
                comboBox1.SelectedItem = value;
                if (value != null)
                    comboBox1.Text = (string)value.Caption;
                _disableEvents = false;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
            if (_disableEvents)
                return;
            if (SelectionChanged != null)
                SelectionChanged(sender, e);
        }

    }
}
